//
//  ItemFromViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 30..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ItemFormViewController.h"


@implementation ItemFormViewController
@synthesize itemTitle = _itemTitle;
@synthesize itemGroupId = _itemGroupId;
@synthesize itemId = _itemId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                  target:self
                                                                                  action:@selector(saveAction)];
        [self.navigationItem setRightBarButtonItem:rightBtn];
        [rightBtn release];
    }
    return self;
}

- (void)dealloc{
    [_itemTitle release];
    [_itemGroupId release];
    [_itemId release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
    _itemTitle.borderStyle = UITextBorderStyleRoundedRect;
    _itemTitle.textColor = [UIColor blackColor];
    _itemTitle.font = [UIFont systemFontOfSize:15.0];
    _itemTitle.backgroundColor = [UIColor whiteColor];
    _itemTitle.autocorrectionType = UITextAutocorrectionTypeNo;
    _itemTitle.keyboardType = UIKeyboardTypeDefault;
    _itemTitle.returnKeyType = UIReturnKeyDone;
    _itemTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
    _itemTitle.placeholder = @"이름을 입력 해 주세요.";

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [self setItemTitle:nil];
    [self setItemGroupId:nil];
    [self setItemId:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveAction{
    if(_isCreateMode){
        [[SQLHandler sharedInstance] createItemWithItemGroupId:_itemGroupId title:[_itemTitle text]];
    }else{
        //    NSLog(@"saveActoin: %d, %@, %d", [_itemId intValue], [_itemTitle text], [_isIn selectedSegmentIndex]);
        [[SQLHandler sharedInstance] updateItemId:_itemId 
                                            title:[_itemTitle text]];
    }
    [[self navigationController] popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadItem:(NSNumber*)itemId{
    _isCreateMode = NO;
    NSDictionary *dic = [[SQLHandler sharedInstance] findItemById:itemId];
    //    NSLog(@"dic:%@",dic);
    self.itemId = itemId;
    [self.itemTitle setText:[dic objectForKey:@"title"]];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)createMode:(NSNumber*)itemGroupId{
    self.itemGroupId = itemGroupId;
    _isCreateMode = YES;
    
}
@end
